//
// DotNetNuke - http://www.dotnetnuke.com
// Copyright (c) 2002-2010
// by Perpetual Motion Interactive Systems Inc. ( http://www.perpetualmotion.ca )
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//

using System;
using System.IO;
using System.Xml;
using System.Xml.XPath;
using DotNetNuke.Common.Utilities;
namespace DotNetNuke.Services.Installer.Installers
{
	public class ConfigInstaller : ComponentInstallerBase
	{
		private string _FileName = Null.NullString;
		private string _InstallConfig = Null.NullString;
		private XmlDocument _TargetConfig;
		private InstallFile _TargetFile;
		private string _UnInstallConfig = Null.NullString;
		private string _UninstallFileName = Null.NullString;
		public string InstallConfig {
			get { return _InstallConfig; }
		}
		public XmlDocument TargetConfig {
			get { return _TargetConfig; }
		}
		public InstallFile TargetFile {
			get { return _TargetFile; }
		}
		public string UnInstallConfig {
			get { return _UnInstallConfig; }
		}
		public override void Commit()
		{
			try {
				Config.Save(TargetConfig, TargetFile.FullName);
				Log.AddInfo(Util.CONFIG_Committed + " - " + TargetFile.Name);
			} catch (Exception ex) {
				Log.AddFailure(ex);
			}
		}
		public override void Install()
		{
			try {
				if (string.IsNullOrEmpty(_FileName)) {
					Util.BackupFile(TargetFile, PhysicalSitePath, Log);
					_TargetConfig = new XmlDocument();
					TargetConfig.Load(Path.Combine(PhysicalSitePath, TargetFile.FullName));
					XmlMerge merge = new XmlMerge(new StringReader(InstallConfig), this.Package.Version.ToString(), this.Package.Name);
					merge.UpdateConfig(TargetConfig);
					Completed = true;
					Log.AddInfo(Util.CONFIG_Updated + " - " + TargetFile.Name);
				} else {
					string strConfigFile = Path.Combine(this.Package.InstallerInfo.TempInstallFolder, _FileName);
					if (File.Exists(strConfigFile)) {
						StreamReader stream = File.OpenText(strConfigFile);
						XmlMerge merge = new XmlMerge(stream, Package.Version.ToString(3), Package.Name + " Install");
						merge.UpdateConfigs();
						stream.Close();
						Completed = true;
						Log.AddInfo(Util.CONFIG_Updated);
					}
				}
			} catch (Exception ex) {
				Log.AddFailure(ex);
			}
		}
		public override void ReadManifest(XPathNavigator manifestNav)
		{
			_FileName = Util.ReadAttribute(manifestNav, "fileName");
			_UninstallFileName = Util.ReadAttribute(manifestNav, "unInstallFileName");
			if (string.IsNullOrEmpty(_FileName)) {
				XPathNavigator nav = manifestNav.SelectSingleNode("config");
				XPathNavigator nodeNav = nav.SelectSingleNode("configFile");
				string targetFileName = nodeNav.Value;
				if (!string.IsNullOrEmpty(targetFileName)) {
					_TargetFile = new InstallFile(targetFileName, "", this.Package.InstallerInfo);
				}
				nodeNav = nav.SelectSingleNode("install");
				_InstallConfig = nodeNav.InnerXml;
				nodeNav = nav.SelectSingleNode("uninstall");
				_UnInstallConfig = nodeNav.InnerXml;
			}
		}
		public override void Rollback()
		{
			Log.AddInfo(Util.CONFIG_RolledBack + " - " + TargetFile.Name);
		}
		public override void UnInstall()
		{
			if (string.IsNullOrEmpty(_UninstallFileName)) {
				_TargetConfig = new XmlDocument();
				TargetConfig.Load(Path.Combine(PhysicalSitePath, TargetFile.FullName));
				XmlMerge merge = new XmlMerge(new StringReader(UnInstallConfig), this.Package.Version.ToString(), this.Package.Name);
				merge.UpdateConfig(TargetConfig, TargetFile.FullName);
			} else {
				string strConfigFile = Path.Combine(this.Package.InstallerInfo.TempInstallFolder, _UninstallFileName);
				if (File.Exists(strConfigFile)) {
					StreamReader stream = File.OpenText(strConfigFile);
					XmlMerge merge = new XmlMerge(stream, Package.Version.ToString(3), Package.Name + " UnInstall");
					merge.UpdateConfigs();
					stream.Close();
				}
			}
		}
	}
}
